package dyy.zc.sportruler.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.util.Util;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.base.BaseFragment;
import dyy.zc.sportruler.bean.StudentInfo;
import dyy.zc.sportruler.listener.OnItemClickListener;
import dyy.zc.sportruler.ui.activity.StudentDetailActivity;
import dyy.zc.sportruler.ui.adapter.TeacherAndStudentAdapter;
import dyy.zc.sportruler.utils.BmobUtils;
import dyy.zc.sportruler.utils.DividerItemDecoration;
import dyy.zc.sportruler.utils.StringUtils;
import dyy.zc.sportruler.utils.ToastUtils;
import dyy.zc.sportruler.utils.Utils;
import dyy.zc.sportruler.views.EmptyView;
import dyy.zc.sportruler.views.WhorlView;

/**
 * Created by chuan on 2017/5/6.
 */

public class StudentFragment extends BaseFragment {
    private TeacherAndStudentAdapter adapter;
    private List<StudentInfo> list = new ArrayList<>();
    private RecyclerView recyclerView;
    private BmobUtils bmobUtils;
    private TextView title;
    private EditText search;
    private View view;
    private WhorlView loading;
    private EmptyView emptyView;
    private boolean isRequestData = true;
    private MaterialRefreshLayout materialRefreshLayout;
    private void findViewsById(View view) {
        Utils.findViewsById(view,R.id.top_rl_back).setVisibility(View.GONE);
        recyclerView  = Utils.findViewsById(view,R.id.recyclerView);
        title = Utils.findViewsById(view,R.id.top_tv_title);
        materialRefreshLayout = Utils.findViewsById(view,R.id.materialRefreshLayout);
        search = Utils.findViewsById(view,R.id.search);
        loading = Utils.findViewsById(view,R.id.loading);
        emptyView = new EmptyView(view);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    loading.stop();
                    loading.setVisibility(View.GONE);
                    if (list.size() == 0){
                        isRequestData = false;
                        emptyView.setNotify("暂无学员");
                    }else {
                        isRequestData = true;
                        emptyView.setEmptyViewGone();
                    }
                    adapter.setList_usp(list);
                    materialRefreshLayout.finishRefreshLoadMore();
                    materialRefreshLayout.finishRefresh();
                    break;
                case 2:
                    loading.stop();
                    loading.setVisibility(View.GONE);
                    list.clear();
                    if (list.size() == 0){
                        emptyView.setNotify("暂无学员");
                    }else {
                        emptyView.setEmptyViewGone();
                    }
                    ToastUtils.shortToast(getActivity(),"网络错误请，稍后重试");
                    break;
            }
        }
    };
    @Override
    protected View onCreateViews(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_student,container,false);
        findViewsById(view);
        initData();
        bind();
        return view;
    }

    private void bind() {

        adapter = new TeacherAndStudentAdapter(getActivity(),list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration decoration = new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST);
        decoration.setDivider(R.drawable.divider_shape);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(), StudentDetailActivity.class);
                intent.putExtra("student",list.get(position));
//                intent.putExtra("type",list.get(position));
                startActivity(intent);
            }
        });

        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String s = search.getText().toString();
//                if (StringUtils.isEmpty(s)){
//                    ToastUtils.shortToast(getActivity(),"不能为空");
//                    return false;
//                }
                if (actionId== EditorInfo.IME_ACTION_SEARCH){
                    bmobUtils.classifyStudent(list,loginuser.getId(),s,1,2,handler);
                    return true;
                }
                return false;
            }
        });


        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                bmobUtils.getStudentInfo(list,loginuser.getId(),1,2,handler);
            }

            @Override
            public void onRefreshLoadMore(final MaterialRefreshLayout materialRefreshLayout) {
                if (isRequestData) {
                    bmobUtils.getStudentInfo(list,loginuser.getId(),1,2,handler);
                    return;
                }
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        materialRefreshLayout.finishRefreshLoadMore();
                    }
                }, 1000);
            }
        });

    }

    private void initData() {
        loading.start();
        title.setText("学员");
        bmobUtils = new BmobUtils(getActivity());
        bmobUtils.getStudentInfo(list,loginuser.getId(),1,2,handler);
    }


}
